package com.licheng.controller;


import com.licheng.anno.Log;
import com.licheng.pojo.Emp;
import com.licheng.pojo.EmpPageQuery;
import com.licheng.pojo.EmpPageResult;
import com.licheng.pojo.Result;
import com.licheng.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    /**
     *分页查询员工
     * @param epq
     * @return
     */
    @GetMapping
    public Result selectEmpPage(EmpPageQuery epq) {

        EmpPageResult empList = empService.selectEmpPage(epq);

        return Result.success(empList);
    }

    /**
     * 删除员工
     * @param ids 员工ids
     * @return
     */
    @Log
    @DeleteMapping()
    public Result deleteEmp(@RequestParam List<Integer> ids) {
        empService.deleteEmp(ids);
        return Result.success();
    }

    /**
     * 插入员工信息
     * @param emp
     * @return
     */
    @Log
    @PostMapping
    public Result add(@RequestBody Emp emp) {

        empService.add(emp);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id) {

        Emp emp = empService.selectById(id);
        return Result.success(emp);
    }

    /**
     * 修改员工
     * @param emp
     * @return
     */
    @Log
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        empService.updateEmp(emp);
        return Result.success();
    }

    /**
     * 查询所有员工
     * @return
     */
    @GetMapping("/list")
    public Result selectAll() {

        List<Emp> empList = empService.selectAll();
        return Result.success(empList);
    }
}
